import axios from 'axios'
import { fromJS } from 'immutable'
import * as constants from './constants'

const changeHomeData = (result) => ({
  type: constants.HOME_DATA,
  topicList: result.topicList,
  articleList: result.articleList,
  recommendList: result.recommendList
})

const addHomeList = (list, nextPage) => ({
  type: constants.ADD_ARTICLE_LIST,
  list: fromJS(list),
	nextPage
})

export const mouseEnterAction = () => ({
  type: constants.MOUSE_ENTER
})

export const mouseLeaveAction = () => ({
  type: constants.MOUSE_LEAVE
})

export const getHomeInfo = () => {
  return (dispatch) => {
    axios.get('/api/home.json').then(res => {
      const result = res.data.data
			dispatch(changeHomeData(result))
		})
  }
}

export const getHomeList = (page) => {
  return (dispatch) => {
    axios.get('/api/homeList.json?page=' + page).then(res => {
      const result = res.data.data
			dispatch(addHomeList(result, page + 1))
		})
  }
}

export const toggleTopShow = (show) => ({
	type: constants.TOGGLE_SCROLL_TOP,
	show
})